const Debounce = require("../template")

function testDebounceBasic() {
  console.log(
    "1. 基本防抖功能测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounce(() => {
        callCount++
      }, 100)
      
      debounced()
      debounced()
      debounced()
      
      return callCount === 0 // 防抖期间不应该调用
    })()
  )

  console.log(
    "2. 立即执行防抖测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounceImmediate(() => {
        callCount++
      }, 100, true)
      
      debounced()
      debounced()
      debounced()
      
      return callCount === 1 // 立即执行应该调用一次
    })()
  )

  console.log(
    "3. 最大等待时间防抖测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounceMaxWait(() => {
        callCount++
      }, 100, 300)
      
      debounced()
      debounced()
      debounced()
      
      return callCount === 0 // 防抖期间不应该调用
    })()
  )

  console.log(
    "4. 可取消防抖测试:",
    (() => {
      let callCount = 0
      const { debounced, cancel } = Debounce.debounceWithCancel(() => {
        callCount++
      }, 100)
      
      debounced()
      debounced()
      cancel()
      
      return callCount === 0 // 取消后不应该调用
    })()
  )

  console.log(
    "5. 可刷新防抖测试:",
    (() => {
      let callCount = 0
      const { debounced, flush } = Debounce.debounceWithFlush(() => {
        callCount++
      }, 100)
      
      debounced()
      debounced()
      flush()
      
      return callCount === 1 // 刷新应该立即调用
    })()
  )

  console.log(
    "6. 状态查询防抖测试:",
    (() => {
      let callCount = 0
      const { debounced, isPending, isCancelled } = Debounce.debounceWithState(() => {
        callCount++
      }, 100)
      
      debounced()
      
      return isPending() === true && isCancelled() === false
    })()
  )

  console.log(
    "7. 条件执行防抖测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounceWithCondition(() => {
        callCount++
      }, 100, (args) => args[0] > 5)
      
      debounced(3) // 不满足条件
      debounced(7) // 满足条件
      debounced(2) // 不满足条件
      
      return callCount === 0 // 防抖期间不应该调用
    })()
  )

  console.log(
    "8. 优先级防抖测试:",
    (() => {
      let callCount = 0
      const highPriorityDebounced = Debounce.debounceWithPriority(() => {
        callCount++
      }, 100, 10)
      
      const lowPriorityDebounced = Debounce.debounceWithPriority(() => {
        callCount++
      }, 100, 1)
      
      lowPriorityDebounced()
      highPriorityDebounced()
      
      return callCount === 0 // 防抖期间不应该调用
    })()
  )

  console.log(
    "9. 批量执行防抖测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounceWithBatch(() => {
        callCount++
      }, 100, 3)
      
      debounced()
      debounced()
      debounced()
      
      return callCount === 0 // 防抖期间不应该调用
    })()
  )

  console.log(
    "10. 错误处理防抖测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounceWithErrorHandling(() => {
        callCount++
        throw new Error('测试错误')
      }, 100, (error) => {
        // 错误处理
      })
      
      debounced()
      
      return callCount === 0 // 防抖期间不应该调用
    })()
  )

  console.log(
    "11. 进度回调防抖测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounceWithProgress(() => {
        callCount++
      }, 100, (progress) => {
        // 进度回调
      })
      
      debounced()
      debounced()
      debounced()
      
      return callCount === 0 // 防抖期间不应该调用
    })()
  )

  console.log(
    "12. 统计功能防抖测试:",
    (() => {
      let callCount = 0
      const { debounced, getStats } = Debounce.debounceWithStats(() => {
        callCount++
      }, 100)
      
      debounced()
      debounced()
      debounced()
      
      const stats = getStats()
      return typeof stats === 'object' && callCount === 0
    })()
  )

  console.log(
    "13. this上下文防抖测试:",
    (() => {
      const debounceObj = {
        multiplier: 2,
        processDebounce: function() {
          const debounced = Debounce.debounce(() => {
            return this.multiplier
          }, 100)
          return debounced()
        }
      }
      return debounceObj.processDebounce() === undefined // 防抖期间返回undefined
    })()
  )

  console.log(
    "14. 复杂对象this绑定防抖测试:",
    (() => {
      const debounceConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function() {
          const debounced = Debounce.debounce(() => {
            return this.config.threshold
          }, 100)
          return debounced()
        }
      }
      return debounceConfigObj.processWithConfig() === undefined // 防抖期间返回undefined
    })()
  )
}

testDebounceBasic()
